﻿using Moe.ECamel.Common.Security;

namespace Moe.ECamel.Common.Entities
{
	using System;
	using System.ComponentModel;
	using System.Linq;

	public partial class User : IEditableObject
	{
		public void SetDefaultValues()
		{
			this.Title = String.Empty;
			this.MiddleName = String.Empty;
			this.HomeAddress = String.Empty;
			this.Telephone = String.Empty;
			this.EmailAddress = String.Empty;
		}

		public ECamelError Validate()
		{
			// Verify mandatory fields:
			string emptyFields = null;
			if (String.IsNullOrEmpty(_UserName))
				emptyFields += "שם משתמש" + Environment.NewLine;
			if (String.IsNullOrEmpty(_FirstName))
				emptyFields += "שם פרטי" + Environment.NewLine;
			if (String.IsNullOrEmpty(_LastName))
				emptyFields += "שם משפחה" + Environment.NewLine;
			if (emptyFields != null)
				return new ECamelError(MessageIds.UserInvalidMissingFields, emptyFields);

			return null;
		}

		public String FullName
		{
			get { return this.FirstName + " " + this.LastName; }
		}

		public UserRoles GetUserRole(int roleId)
		{
			return this.UserRoles.SingleOrDefault(r => r.RoleId == roleId);
		}
		public bool HasRole(int roleId)
		{
			return GetUserRole(roleId) != null;
		}
		public void AddRole(int roleId)
		{
			this.UserRoles.Add(new UserRoles { UserId = this.UserId, RoleId = roleId });	
		}
		public int GetPrimaryRole()
		{
			int[] prioritizedRoles = new int[] { Roles.Admin, Roles.Manager, Roles.Employee, Roles.Customer };
			foreach (var role in prioritizedRoles)
				if (HasRole(role))
					return role;

			return -1;
		}

		#region IEditableObject Members

		private User _originalEntity = null;

		public void BeginEdit()
		{
				if (this._originalEntity == null)
			{
				// Initialize original library 
				this._originalEntity = new User();
				// Copy editable properties values:
				this._originalEntity._UserName = this._UserName;
				this._originalEntity._Title = this._Title;
				this._originalEntity._FirstName = this._FirstName;
				this._originalEntity._MiddleName = this._MiddleName;
				this._originalEntity._LastName = this._LastName;
				this._originalEntity._EmailAddress = this._EmailAddress;
				this._originalEntity._Telephone = this._Telephone;
				this._originalEntity._HomeAddress = this._HomeAddress;
			}
		}

		public void CancelEdit()
		{
			if (this._originalEntity != null)
			{
				// Restore properties:
				this._UserName = this._originalEntity._UserName;
				this._Title = this._originalEntity._Title;
				this._FirstName = this._originalEntity._FirstName;
				this._MiddleName = this._originalEntity._MiddleName;
				this._LastName = this._originalEntity._LastName;
				this._EmailAddress = this._originalEntity._EmailAddress;
				this._Telephone = this._originalEntity._Telephone;
				this._HomeAddress = this._originalEntity._HomeAddress;
				// Reset original values:
				this._originalEntity = null;
			}
		}

		public void EndEdit()
		{
			// Reset original values:
			this._originalEntity = null;
		}

		#endregion
	}
}